{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sort Colors\n",
    "\n",
    "[原题](https://mp.weixin.qq.com/s/TbFvpFW_AXxmSQE4I1VKzw)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.\n",
    "\n",
    "Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.\n",
    "\n",
    "***Note***: *You are not suppose to use the built-in function `sort()` for this problem.*\n",
    "\n",
    "Can you do this in a single pass?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "```text\n",
    "Input: [2,0,2,1,1,0]\n",
    "Output: [0,0,1,1,2,2]\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Class Definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def sortColors(self, nums):\n",
    "        ''' Main Algorithm '''\n",
    "        \n",
    "        # Define 2 index pointer\n",
    "        begin = 0\n",
    "        end = len(nums) - 1\n",
    "        \n",
    "        # Define a working pointer\n",
    "        k = 0\n",
    "        \n",
    "        # Browse all elements in the list\n",
    "        while k <= end:\n",
    "            if nums[k] == 0:\n",
    "                # Swap with the first element\n",
    "                (nums[k], nums[begin]) = (nums[begin], nums[k])\n",
    "                \n",
    "                # Lock these elements with the value of 0\n",
    "                begin += 1\n",
    "                \n",
    "                # Move to next element\n",
    "                k += 1\n",
    "            elif nums[k] == 2:\n",
    "                # Swap with the last element\n",
    "                (nums[k], nums[end]) = (nums[end], nums[k])\n",
    "                \n",
    "                # Lock these elements with the value of 2\n",
    "                end -= 1\n",
    "            else:\n",
    "                # Move to next element directly\n",
    "                k += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Main Scripts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before Sort: \n",
      "[0, 1, 2, 2, 1, 1, 2, 2, 0, 0, 0, 0, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "nums = [0, 1, 2, 2, 1, 1, 2, 2, 0, 0, 0, 0, 2, 1]\n",
    "print(\"Before Sort: \")\n",
    "print(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After Sort: \n",
      "[0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2]\n"
     ]
    }
   ],
   "source": [
    "Solution().sortColors(nums)\n",
    "print(\"After Sort: \")\n",
    "print(nums)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
